package com.kiln.juxingqs.ui

import android.view.ViewGroup
import client.global.commlib.base.SyBannerAdapter
import client.global.commlib.getScreenWidth
import com.bumptech.glide.request.RequestOptions
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.PpZrDetailAdapter
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.databinding.ActivityPpzrDetailBinding
import com.kiln.juxingqs.ui.vm.TestViewModel
import com.youth.banner.indicator.CircleIndicator

class PpZrDetailActivity : BaseMiddleActivity<TestViewModel, ActivityPpzrDetailBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_ppzr_detail
    }

    val adapter: PpZrDetailAdapter by lazy {
        PpZrDetailAdapter(this, mImgLoader)
    }

    override fun initView() {
        binding?.apply {
            event = this@PpZrDetailActivity
            recyclerView.adapter = adapter
        }
        val width = getScreenWidth(this)
        val params: ViewGroup.LayoutParams = binding.banner.getLayoutParams()
        params.height = width //(width * 575 / 1400f).toInt()
        binding.banner.setLayoutParams(params)
        binding.banner.setIndicator(CircleIndicator(this))

    }

    override fun initData() {
        var bannerList = intent.getParcelableArrayListExtra<GoodsDetail.Banner>("banner_list")
        val detailList = intent.getParcelableArrayListExtra<GoodsDetail.Detail>("detail_list")

        if (bannerList != null) {
            binding.banner.setAdapter(object : SyBannerAdapter<GoodsDetail.Banner>(bannerList) {
                override fun onBindView(
                    holder: BannerViewHolder?,
                    data: GoodsDetail.Banner?,
                    position: Int,
                    size: Int
                ) {
                    mImgLoader.load(data?.goods_img)
                        .apply(RequestOptions().error(R.mipmap.ic_launcher)).into(
                            holder!!.imageView
                        )
                }
            })
        }
        binding.banner.start()


        val list = ArrayList<String>()
        for (d in detailList!!) {
            list.add(d.goods_img)
        }
        adapter.submitList(list)
    }

    override fun initViewModel(): TestViewModel = createViewModel()

}